/*  // copy below to main to test
    let b = ch_035_finite_state_automation__hard::recognize_pattern("abbbbbbc");

    let result = ch_035_finite_state_automation__hard::recognize_pattern("abbbc");
    assert_eq!(result, true);

    let result = ch_035_finite_state_automation__hard::recognize_pattern("ac");
    assert_eq!(result, true);

    let result = ch_035_finite_state_automation__hard::recognize_pattern("abbbd");
    assert_eq!(result, false);

    let result = ch_035_finite_state_automation__hard::recognize_pattern("");
    assert_eq!(result, false);
*/

pub fn recognize_pattern(input: &str) -> bool {
    // Implement your finite state automaton here
    let mut r: bool = true;
    let mut cl: Vec<char> = input.to_string().chars().collect();
    // println!("{}", cl[0].eq(&'a'));
    // println!("{}", cl[cl.len() - 1].eq(&'c'));
    match cl.len() {
        0 => {
            r = false;
        }
        1 => {
            r = false;
        }
        _ => {
            //println!("1 {:?}", cl);
            let mut left = cl.split_off(1);
            if !cl[0].eq(&'a') {
                r = false;
            }
            //println!("2 {:?}", left);
            let last = left.split_off(left.len() - 1);
            if !last[0].eq(&'c') {
                r = false;
            }
            //println!("3 {:?}", left);
            left.dedup();
            //println!("4 {:?}", left);
            if left.len() != 0 && (left.len() > 1 || !left.contains(&'b')) {
                r = false;
            }
        }
    }
    // if cl.len() > 2 {
    // let first = cl.split_off(1);
    // let last = cl.split_off(cl.len() - 1);
    // cl.dedup();
    // if cl.len() != 1 || !cl.contains(&'b') {
    //     r = false;
    // }
    // }

    r
}

/* -- standard answer
#[derive(PartialEq)]
enum State {
    A,
    B,
    C
}
pub fn recognize_pattern(input: &str) -> bool {
    if input.is_empty() {
        return false;
    }
    let mut state = State::A;
    for c in input.chars() {
        match state {
            State::A => match c {
                'a' => state = State::B,
                _ => return false,
            },
            State::B => match c {
                'b' => continue,
                'c' => state = State::C,
                _ => return false,
            },
            State::C => return false,
        }
    }
    state == State::C
}

*/
